package com.jjh.web.controller;

import com.jjh.dao.impl.DishDaoImpl;
import com.jjh.pojo.Shop;
import com.jjh.service.impl.DishServiceImpl;
import com.jjh.service.impl.OrderServiceImpl;
import com.jjh.service.impl.UserServiceImpl;
import com.jjh.winterframework.annatation.Component;
import com.jjh.dao.DishDao;
import com.jjh.pojo.Dishes;
import com.jjh.pojo.Order;
import com.jjh.pojo.User;
import com.jjh.service.OrderService;
import com.jjh.util.RandomUtils;
import com.jjh.util.ViewUtils;
import com.jjh.Application;
import com.jjh.web.view.OrderView;
import com.jjh.winterframework.annatation.Autowired;
import com.jjh.wrapper.QueryWrapper;

import java.time.LocalDateTime;
import java.util.Scanner;
@Component
public class OrderController implements OrderView {

    private static Scanner scanner =new Scanner(System.in);

    //自己的dao
    @Autowired
    private DishServiceImpl dishService;
    //调用订单服务
    @Autowired
    private OrderServiceImpl orderService ;

    @Autowired
    private DishController dishController ;

    @Autowired
    private UserServiceImpl userService;

    //调用用户服务
    //private UserSerice userSerice = new UserServiceImpl();


    /**
     * 点餐
     * @param dishes 菜品
     */
    @Override
    public void orderOne(Dishes dishes, int page, int size, QueryWrapper wrapper, Shop shop) {
        boolean flag = true;
        int num;
        double allprice;
        String address;
        String desc;
        while (flag) {
            System.out.println(" -> 你确定要点该菜品吗？(yes/no)");
            String chince = scanner.nextLine();
            if (chince.equalsIgnoreCase("no")) {
                //返回之前的页面
                dishController.showDishes(shop);
                break;
            } else if (chince.equalsIgnoreCase("yes")) {
                System.out.println(" -> 你选择的菜品为: " + dishes.getDname());
                System.out.println(" -> 单价为: " + dishes.getPrice() + " 剩余: " + dishes.getNum());
                if(dishes.getCount()==0){
                    System.out.println("====>该菜品已经售空！请选择其他菜品！");
                    dishController.showDishes(shop);
                    break;
                }
                while (true) {
                    System.out.print(" -> 请输入你要购买的份数:");
                    num = scanner.nextInt();
                    if (num < 0 ) {
                        System.out.println("=====>输入份数不合法！请重新输入:");
                    } else {
                        break;
                    }
                }
                allprice = dishes.getPrice() * num;
                System.out.println(" ---> 你要购买:" + num + "份" + dishes.getDname()
                        + "总价为:" + allprice);
                System.out.println("------------------------------------------------------------");
                System.out.println(" -> 请填写收货地址: (0为默认收货地址)");
                scanner.nextLine();
                String addr = scanner.nextLine();


                if (addr.equals("0")) {
                    User user = Application.threadLocal.get();
                    address = userService.getAddress(user.getId());
                } else {
                    address = addr;
                }
                System.out.println("---------------------------------------------------------------");
                System.out.println(" -> 请输入备注:");
                desc = scanner.nextLine();
                System.out.println("------------------------------------------------------------------");
                System.out.println(" -> 请付款===>" + allprice + "元");
                while (true) {
                    System.out.println(" -> 输入对应价格来模拟付款:");
                    double price = scanner.nextDouble();
                    if (price == allprice) {
                        System.out.println("===========>付款成功！,正在生成订单....");
                        //向订单表中插入一条数据
                        Order order = new Order();
                        order.setOId(RandomUtils.getId());
                        order.setShopId(dishes.getShopId());
                        order.setDishId(dishes.getDId());
                        order.setDName(dishes.getDname());
                        order.setCount(num);
                        order.setSinglePrice(dishes.getPrice());
                        order.setAllPrice(allprice);
                        order.setAddress(address);
                        order.setDesc(desc);
                        order.setTime(LocalDateTime.now());
                        //下单插入一条数据
                        orderService.insertOne(order);
                        //扣库存
                        dishService.removeCount(dishes,num);
                        //模拟订单处理时间
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        ViewUtils.printOrderSuccess();
                        System.out.println();
                        System.out.println("====> 你已经成功点餐，你可以在个人中心中查看订单信息，欢迎再次点餐!");
                        //返回之前的页面
                        System.out.print(" -> 请输入0返回上一级：");
                        while (true){
                            scanner.nextLine();
                            String flag2 =scanner.nextLine();
                            if(flag2.equals("0")){
                                dishController.showDishes(shop);
                                break;
                            }
                            System.out.println("=====>输入不合法，请重新输入:");
                        }
                        flag = false;
                        break;
                    } else {
                        System.out.println("=====>输入金额不正确，请重新输入:");
                    }
                }
            } else {
                System.out.println("======>输入错误，请重新输入:");
            }
        }

    }

}
